package cn.shop.dys.util;
import android.content.Context;
import android.util.Log;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.db.sqlite.WhereBuilder;
import com.lidroid.xutils.exception.DbException;
import java.util.List;

import cn.shop.dys.bean.SearchHistoryInfo;

/**
 * 作者：jian on 2016/4/21 09:04
 * 孙建
 */
public class SearchHistoryInfoDBUtil {
    private static DbUtils dbUtils;

    public static void initDB(Context context, String dbName){
        dbUtils = DbUtils.create(context, dbName);
    }
    public static void saveSearchHistoryInfo(SearchHistoryInfo searchHistoryInfo){
        try {
            SearchHistoryInfo mySearchHistoryInfo = dbUtils.findFirst(Selector.from(
                    SearchHistoryInfo.class).where("search_content", "=", searchHistoryInfo.getSearch_content()));
            Log.d("test","进行存储");
            if(mySearchHistoryInfo==null){
                dbUtils.save(searchHistoryInfo);
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static List<SearchHistoryInfo> queryAllSearchHistoryInfo(){
        List<SearchHistoryInfo> infos=null;
        try {
            infos=dbUtils.findAll(SearchHistoryInfo.class);
          //  infos = dbUtils.findAll(Selector.from(SearchHistoryInfo.class).where("search_content", "=", jid));
        } catch (DbException e) {
            e.printStackTrace();
        }
        return infos;
    }
    public static void deleteSearchHistoryInfoBySearchContent(String searchContent){
        WhereBuilder whereBuilder = WhereBuilder.b("search_content", "=", searchContent);
        try {
            dbUtils.delete(SearchHistoryInfo.class,whereBuilder );
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
    public static void clearAllSearchHistoryInfo(){
        try {
            dbUtils.deleteAll(SearchHistoryInfo.class);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
